package com.kaifamiao.wendao.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Topic;
import com.kaifamiao.wendao.service.TopicService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/topic/*")
public class TopicServlet extends HttpServlet {

    private TopicService topicService = new TopicService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String sid = req.getParameter("id");
        if(sid == null || sid.isBlank()){
            List<Topic> topics = topicService.loadAll();
            out.println(JSONObject.toJSONString(Map.of("topics",topics)));
            return;
        }
        Long id = 0L;
        try {
            id =Long.valueOf(sid);
        } catch (NumberFormatException e) {
            out.println(JSONObject.toJSONString(Map.of("message","不符合的查询")));
        }

        Topic topic = topicService.load(id);
        String topic1 = JSONObject.toJSONString(Map.of("topic", topic == null ? "" : topic));
        out.println(topic1);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        PrintWriter out = resp.getWriter();
        Topic topic = null;
        try {
            String title = req.getParameter("title");
            String content = req.getParameter("content");
            //令牌
            String custonerId = req.getParameter("custonerId");
            Customer customer = new Customer();
            customer.setId(Long.valueOf(custonerId));
            topic = new Topic();
            topic.setTitle(title);
            topic.setContent(content);
            topic.setAuthor(customer);
            topic.setPublishAddress(req.getRemoteAddr());
        } catch (Exception e) {
            map.put("success",false);
            map.put("message","获取参数失败");
            out.println(JSONObject.toJSONString(map));
            return;
        }
        Boolean save = topicService.save(topic);
        if(save){
            map.put("success",true);
            map.put("message","保存成功！");
            out.println(JSONObject.toJSONString(map));
            return;
        }
        map.put("success",false);
        map.put("message","保存失败！");
        out.println(JSONObject.toJSONString(map));
        return;
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();
        PrintWriter out = resp.getWriter();
        Topic topic = null;
        try {
            String sid = req.getParameter("id");
            String title = req.getParameter("title");
            String content = req.getParameter("content");
            Long id = Long.valueOf(sid);
            //令牌
            String custonerId = req.getParameter("custonerId");
            Customer customer = new Customer();
            customer.setId(Long.valueOf(custonerId));
            topic = new Topic();
            topic.setTitle(title);
            topic.setContent(content);
            topic.setId(id);
            topic.setAuthor(customer);
            topic.setPublishAddress(req.getRemoteAddr());
            topic.setPublishTime(LocalDateTime.now());
        } catch (Exception e) {
            map.put("success",false);
            map.put("message","获取参数失败");
            out.println(JSONObject.toJSONString(map));
            return;
        }
        Boolean save = topicService.update(topic);
        if(save){
            map.put("success",true);
            map.put("message","修改成功！");
            out.println(JSONObject.toJSONString(map));
            return;
        }
        map.put("success",false);
        map.put("message","修改失败！");
        out.println(JSONObject.toJSONString(map));
        return;
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String sid = req.getParameter("id");
        Map<String, Object> map = new HashMap<>();
        if(sid == null || sid.isEmpty() || sid.isBlank()){
            map.put("success",false);
            map.put("message","传入id错误");
            out.println(JSONObject.toJSONString(map));
            return;
        }
        Long id =null;
        try {
            id = Long.valueOf(sid);
        } catch (NumberFormatException e) {
            map.put("success",false);
            map.put("message","id错误");
            out.println(JSONObject.toJSONString(map));
            return;
        }

        boolean remove = topicService.remove(id);
        if(remove){
            map.put("success",true);
            map.put("message","删除成功！");
            System.out.println("成功！");
        }else{
            map.put("success",false);
            map.put("message","删除失败！");
        }
        out.println(JSONObject.toJSONString(map));
        return;
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain;charset=utf-8");
        super.service(req,resp);
    }
}
